import { Card, Col, List, Popover, Row, Statistic,Progress } from 'antd';
import {Chart,Interval,Line,Coordinate,Axis,Tooltip} from 'bizcharts';
import ScheduleItemsController from '@/services/scheduleItems/ScheduleItemsController'
import InnovationStudioController from '@/services/InnovationStudio/InnovationStudioController'
import React from 'react';
import QCProjectController from '@/services/QCProject/QCProjectController';
import InnovationProjectController from '@/services/InnovationProject/InnovationProjectController';
import MembersController from '@/services/Members/MembersController';
import OfferedController from '@/services/OfferedProject/OfferedController';
class index extends React.Component {
    constructor(props) {
        super(props);
        this.state = { 
            userInfo:JSON.parse(sessionStorage.getItem('userInfo')),
            data2: [
                {item:'已完成',percent:0.71},
                {item:'未完成',percent:0.29},
            ],
            data4: [
                {item:'已完成',percent:0.71},
                {item:'未完成',percent:0.29},
            ],
            innovationStatistic:[],
            qcStatistic:[],
            studioCount:0,
            innovationCount:0,
            qcCount:0,
            leanCount:0,
            offeredCound:0,
            offeredScore:0,
            data : [
                {genre:'一月',sold:0},
                {genre:'二月',sold:0},
                {genre:'三月',sold:0},
                {genre:'四月',sold:0},
                {genre:'五月',sold:0},
                {genre:'六月',sold:0},
                {genre:'七月',sold:0},
                {genre:'八月',sold:0},
                {genre:'九月',sold:0},
                {genre:'十月',sold:0},
                {genre:'十一月',sold:0},
                {genre:'十二月',sold:0},
            ],
            showOffered:false,
            data5 : [
                {genre:'一月',sold:0},
                {genre:'二月',sold:0},
                {genre:'三月',sold:0},
                {genre:'四月',sold:0},
                {genre:'五月',sold:0},
                {genre:'六月',sold:0},
                {genre:'七月',sold:0},
                {genre:'八月',sold:0},
                {genre:'九月',sold:0},
                {genre:'十月',sold:0},
                {genre:'十一月',sold:0},
                {genre:'十二月',sold:0},
            ],
            showOfferedRecord:false
         };
    }
    componentDidMount(){
        this.getInnovationExecutionList();
        this.getStudioList();
        this.getProjectCount();
        this.getScore();
        this.getQcExecutionList();
        this.getOfferedStatistic();
        this.getOfferedRcordStatistic();
        this.getOfferedCount();
    }
    getStudioList=()=>{
        let {userInfo,data2} = this.state
        let query = {}
        query.userName=userInfo.userName
        query.studioStatus= "正常"
        InnovationStudioController.getList(query).then(r=>{
            if(r&&r.data){
                this.setState({
                    studioCount:r.data.total
                })
            }
        })
    }
    // 创新项目完成率
    getInnovationExecutionList = ()=>{
        let {userInfo,data2} = this.state
        let query = {}

        query.userName=userInfo.userName
        ScheduleItemsController.getExecutionList(query).then((result) => {
            if(result&&result.data&&result.data.total>0){
            let complete = (result.data.rows.filter(item=>item.files).length/result.data.total).toFixed(2)
            let uncomplete = (1-complete).toFixed(2)
                data2[0].percent = Number(complete)
                data2[1].percent = Number(uncomplete)
            let innovationStatistic = this.getStatistic(result.data.rows)
            // console.log(innovationStatistic)
        this.setState({data2,innovationStatistic})
        }
        })
    }
    getStatistic=(dataList)=>{
        let projectList = []
        dataList.forEach(item=>{
            
            if(projectList.filter(r=>r.projectCode === item.projectCode).length===0){
                projectList.push({projectCode:item.projectCode,projectName:item.projectName})
            }
        })
        projectList.forEach((item,index)=>{
            projectList[index].total = dataList.filter(r=>r.projectCode === item.projectCode).length
            projectList[index].percent = dataList.filter(r=>r.projectCode === item.projectCode).filter(m=>m.files).length
        })
  

        return projectList
    }
    // 创新项目完成率
    getProjectCount = ()=>{
        let {userInfo,data2} = this.state

        MembersController.getProjectCount(userInfo.userName).then((result) => {
            if(result&&result.data){
                let innovationCount = 0
                let qcCount = 0
                let leanCount = 0
                
                result.data.forEach(item=>{
                   if(item.projectType==="innovation"){innovationCount = item.count}
                   if(item.projectType==="qc"){qcCount = item.count}
                   if(item.projectType==="lean"){leanCount = item.count}
                   
                })
        this.setState({innovationCount,qcCount,leanCount})
        }
        })
    }
    getOfferedCount=()=>{
        let {userInfo,data4} = this.state
        let query = {}
        query.userName=userInfo.userName
        OfferedController.getApplyRecordList(query).then(r=>{
            this.setState({
                offeredCound:r.data.total
            })
        })
    }
getQcExecutionList=()=>{
    let {userInfo,data4} = this.state
        let query = {}

        query.userName=userInfo.userName
    QCProjectController.getExecutionList(query).then((result) => {
        if(result&&result.data&&result.data.total>0){
        let complete = (result.data.rows.filter(item=>item.files).length/result.data.total).toFixed(2)
        let uncomplete = (1-complete).toFixed(2)
        data4[0].percent = Number(complete)
        data4[1].percent = Number(uncomplete)
        let qcStatistic = this.getStatistic(result.data.rows)
     
    this.setState({data4,qcStatistic})
    }
    })
}
    getScore=()=>{
        let {userInfo,data2} = this.state
        OfferedController.getScore(userInfo.userName).then(r=>{
            if(r&&r.data&&r.data[0]){
                this.setState({
                    offeredScore:r.data[0]
                })
            }
        })
    }
    getOfferedStatistic=()=>{
        let {userInfo,data} = this.state
        let params = {
            userName:userInfo.userName,
            year:2020,
            lever:'市级'
        }
        OfferedController.getOfficeStatistic(params).then(r=>{
            if(r&&r.data.length>0){
                r.data.forEach(item=>{
                    switch(item.m){
                        case 1:
                            data[0].sold = item.counts
                            break;
                        case 2:
                            data[1].sold = item.counts
                            break;
                        case 3:
                            data[2].sold = item.counts
                            break;
                        case 4:
                            data[3].sold = item.counts
                            break;
                        case 5:
                            data[4].sold = item.counts
                            break;
                        case 6:
                            data[5].sold = item.counts
                            break;
                        case 7:
                            data[6].sold = item.counts
                            break;
                        case 8:
                            data[7].sold = item.counts
                            break;
                        case 9:
                            data[8].sold = item.counts
                            break;
                        case 10:
                            data[9].sold = item.counts
                             break;
                        case 11:
                        
                            data[10].sold = item.counts
                           
                            break;
                        case 12:
                            data[11].sold = item.counts
                            break;
                    }
                    this.setState({data,showOffered:true})
                })
            }
        })
    }
    getOfferedRcordStatistic=()=>{
        let {userInfo,data5} = this.state
        let params = {
            userName:userInfo.userName,
            year:2020,
            lever:'市级'
        }
        OfferedController.getOfficeRecordStatistic(params).then(r=>{
            if(r&&r.data.length>0){
                r.data.forEach(item=>{
                    switch(item.m){
                        case 1:
                            data5[0].sold = item.counts
                            break;
                        case 2:
                            data5[1].sold = item.counts
                            break;
                        case 3:
                            data5[2].sold = item.counts
                            break;
                        case 4:
                            data5[3].sold = item.counts
                            break;
                        case 5:
                            data5[4].sold = item.counts
                            break;
                        case 6:
                            data5[5].sold = item.counts
                            break;
                        case 7:
                            data5[6].sold = item.counts
                            break;
                        case 8:
                            data5[7].sold = item.counts
                            break;
                        case 9:
                            data5[8].sold = item.counts
                            break;
                        case 10:
                            data[9].sold = item.counts
                             break;
                        case 11:
                        
                            data5[10].sold = item.counts
                           
                            break;
                        case 12:
                            data[11].sold = item.counts
                            break;
                    }
                    this.setState({data5,showOfferedRecord:true})
                })
            }
        })
    }
    
    render() {
        const {data2,studioCount,innovationCount,qcCount,leanCount,offeredCound,offeredScore,innovationStatistic,data4,qcStatistic,data,data5,showOffered,showOfferedRecord} = this.state
        
    
        const cols ={
            percent:{
                formatter:val=>{
                    val=val*100+"%"
                    return val
                }
            }
        }
        return (
            <div>
                <Row gutter={[8,8]}>
                    <Col span={8}>
                        <Card title={"我参与的工作室"}>
                            <Statistic title="工作室数量" value={`${studioCount}个`} />
                        </Card>
                    </Col>
                    <Col span={8}>
                        <Card title={"我参与的项目统计"}>
                            <Statistic title="创新项目" value={`${innovationCount}个`} style={{width:130,float:'left'}}/>
                            <Statistic title="精益项目" value={`${leanCount}个`} style={{width:130,float:'left'}} />
                            <Statistic title="QC课题" value={`${qcCount}个`} style={{width:130,float:'left'}} />
                            <Statistic title="悬赏项目" value={`${offeredCound}个`} style={{width:130,float:'left'}} />
                        </Card>
                        </Col>
                        
                    <Col span={8}>
                        <Card title={"我的悬赏得分"}>
                            <Statistic title="悬赏项目总得分" value={`${offeredScore}分`} />
                        </Card>                 
                    </Col>

                    <Col span={8}>
                        <Card title={"创新项目完成率"}>
                            <Chart height={400} autoFit data={data2} scale={cols}>
                                <Coordinate type={"theta"} radius={0.75}/>
                                <Axis visible={false}/>
                                <Interval
                                    position="percent"
                                    adjust="stack"
                                    color="item"
                                    style={{
                                        lineWidth:1,
                                        stroke:'#fff',
                                    }}
                                    label={['*',{
                                        content:(data2)=>{
                                            return `${data2.item}：${(data2.percent * 100).toFixed(2)}%`
                                        }
                                    }]}
                                />
                            </Chart>
                        </Card>
                    </Col>
               
                    <Col span={8}>
                        <Card title={"QC课题完成率"}>
                            <Chart height={400} autoFit data={data4} scale={cols}>
                                <Coordinate type={"theta"} radius={0.75}/>
                                <Axis visible={false}/>
                                <Interval
                                    position="percent"
                                    adjust="stack"
                                    color="item"
                                    style={{
                                        lineWidth:1,
                                        stroke:'#fff',
                                    }}
                                    label={['*',{
                                        content:(data4)=>{
                                            return `${data4.item}：${(data4.percent * 100).toFixed(2)}%`
                                        }
                                    }]}
                                />
                            </Chart>
                        </Card>
                    </Col>
                    <Col span={8}>
                        <Card title={"创新项目进度"}>
                                {innovationStatistic.map(item=><div><strong>{item.projectName}</strong><Progress percent={(Number(item.percent)/Number(item.total)*100).toFixed(2)} /></div>)}
                        </Card>
                    </Col>
                    <Col span={8}>
                        <Card title={"QC项目进度"}>
                        {qcStatistic.map(item=><div><strong>{item.projectName}</strong><Progress percent={(Number(item.percent)/Number(item.total)*100).toFixed(2)} /></div>)}
                        </Card>
                    </Col>
           
                    <Col span={8}>
                        <Card title={"悬赏项目发布量"}>
                            {showOffered&&<Chart height={250} autoFit data={data}>
                                <Interval position="genre*sold"/>
                            </Chart>}
                        </Card>
                    </Col>
                    <Col span={8}>
                        <Card title={"悬赏项目接收量"}>
                            {showOfferedRecord&&<Chart height={250} autoFit data={data5}>
                                <Line position="genre*sold"/>
                            </Chart>}
                        </Card>
                    </Col>
                    {/* <Col span={8}>
                        <Card title={"悬赏项目进度"}>
                            <strong>北斗卫星导航计划</strong><Progress percent={30} />
                            <strong>蛟龙深海潜器计划</strong><Progress percent={50} />
                            <strong>焊接工艺改良计划</strong><Progress percent={70}  />
                         
                        </Card>
                    </Col> */}
                </Row>
            </div>
        );
    }
}

export default index;